<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script>
        function createPerson(name, age, gender) {
            this.name = name;
            this.age = age;
            this.gender = gender;

            // this.sayName;
        }

        //使所有对象共享一个方法

        /*
        //将sayname方法在全局作用域中定义,污染了全局作用域的命名空间
         function fun() {
            alert(this.name)
        }
        */

        //在原型中添加sayname方法
        createPerson.prototype.sayName = function () {
            alert(this.name)
        }


        var per = new createPerson("猪八戒", 25, "男");
        var per2 = new createPerson("蜘蛛精", 15, "女");
        var per3 = new createPerson("白骨精", 35, "女");

        //每执行一次构造函数，就会创建一个新的方法
        per.sayName();
        per2.sayName();
        per3.sayName();
    </script>
</head>

<body>

</body>

</html>